package org.example.src.controller;

import org.example.src.dto.request.ApplicationRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.ApplicationResponse;
import org.example.src.entity.Application;
import org.example.src.entity.ApplicationStatus;
import org.example.src.service.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/applications")
@CrossOrigin(origins = "*")
public class ApplicationController extends BaseController {

    @Autowired
    private ApplicationService applicationService;

    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResponseEntity<ApiResponse<List<Application>>> getAllApplications() {
        List<Application> applications = applicationService.findAllApplications();
        return success(applications);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'COMPANY', 'TEACHER')")
    public ResponseEntity<ApiResponse<Application>> getApplicationById(@PathVariable Long id) {
        Application application = applicationService.findApplicationById(id);
        if (application == null) {
            return notFound("申请未找到");
        }
        return success(application);
    }

    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<Application>> createApplication(@Valid @RequestBody ApplicationRequest request) {
        Application createdApplication = applicationService.createApplication(request);
        return created(createdApplication);
    }

    @PutMapping("/{id}/status")
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<Application>> updateApplicationStatus(
            @PathVariable Long id,
            @RequestParam ApplicationStatus status,
            @RequestParam(required = false) String feedback) {
        Application updatedApplication = applicationService.updateApplicationStatus(id, status, feedback);
        return success("申请状态更新成功", updatedApplication);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<Void>> deleteApplication(@PathVariable Long id) {
        applicationService.deleteApplication(id);
        return noContent();
    }

    @GetMapping("/student/{studentId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<Application>>> getApplicationsByStudentId(@PathVariable Long studentId) {
        List<Application> applications = applicationService.findApplicationsByStudentId(studentId);
        return success(applications);
    }

    @GetMapping("/job-post/{jobPostId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<List<Application>>> getApplicationsByJobPostId(@PathVariable Long jobPostId) {
        List<Application> applications = applicationService.findApplicationsByJobPostId(jobPostId);
        return success(applications);
    }

    @GetMapping("/company/{companyId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'COMPANY')")
    public ResponseEntity<ApiResponse<List<Application>>> getApplicationsByCompanyId(@PathVariable Long companyId) {
        List<Application> applications = applicationService.findApplicationsByCompanyId(companyId);
        return success(applications);
    }

    @GetMapping("/student/{studentId}/responses")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<ApplicationResponse>>> getApplicationResponsesByStudentId(@PathVariable Long studentId) {
        List<ApplicationResponse> applications = applicationService.getApplicationsByStudentId(studentId);
        return success(applications);
    }
}
